/**
 * 
 */
package com.net.shop.car.manager.api.volley.response;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.net.shop.car.manager.api.model.Goods;
import com.net.shop.car.manager.api.model.SellerInfo;
import com.net.shop.car.manager.api.volley.Response;
import com.net.shop.car.manager.utils.Constants;
import com.net.shop.car.manager.utils.LogUtils;
import com.net.shop.car.manager.utils.StringUtils;

/**
 * @author Deve
 *
 */
public class GetWaySaveResponse extends Response {
	
	/**
	 * 
	 */
	private SellerInfo sellerInfo;
	
	/**
	 * 
	 */
	private LujiuService dayService = new LujiuService();
	
	/**
	 * 
	 */
	private LujiuService nightService = new LujiuService();

	/**
	 * @param type
	 */
	public GetWaySaveResponse() {
		super(Constants.WAY_SAVE);
	}

	@Override
	public String[] getContentTags() {
		// TODO Auto-generated method stub
		return new String[] { "seller", "service_day", "service_night"};
	}

	@Override
	public void parseContent(String content, int index) {
		switch (index) {
		case 0:
			parseSeller(content);
			break;
		case 1:
			parseService(content,dayService);
			break;
		case 2:
			parseService(content,nightService);
			break;
		default:
			break;
		}
	}
	
	private void parseService(String content, LujiuService serviceGoods) {
		try {
			JSONObject jsonObject = new JSONObject(content);
			serviceGoods.setServiceTime(StringUtils.filterNull(jsonObject.getString("time")));
			String serviceStr = StringUtils.filterNull(jsonObject.getString("goods"));
			JSONArray jsonArray = new JSONArray(serviceStr);
			List<Goods> serviceList = new ArrayList<Goods>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				Goods goods = new Goods();
				goods.setCheapPrice(StringUtils.filterMoneyNull(object.getString("CHEAP_PRICE")));
				goods.setGoodId(StringUtils.filterNull(object.getString("ID")));
				goods.setGoodName(StringUtils.filterNull(object.getString("GOOD_NAME")));
				goods.setSupportVB(StringUtils.filterBoolNull(object.getString("IS_VB")));
				goods.setPrice(StringUtils.filterMoneyNull(object.getString("PRICE")));
				serviceList.add(goods);
			}
			serviceGoods.setServiceGoods(serviceList);
		} catch (JSONException e) {
			LogUtils.e(e.getMessage());
		}
	}

	private void parseSeller(String content) {
		try {
			JSONObject jsonObject = new JSONObject(content);
			sellerInfo = new SellerInfo();
			sellerInfo.setErrNum(StringUtils.filterIntegerNull(jsonObject.getString("ERR_NUM")));
			sellerInfo.setShopAddress(StringUtils.filterNull(jsonObject.getString("SHOP_ADDR")));
			sellerInfo.setIntroduce(StringUtils.filterNull(jsonObject.getString("INTRODUCE")));
			sellerInfo.setTels(StringUtils.filterTelsNull(jsonObject.getString("TEL")));
			sellerInfo.setSellerId(StringUtils.filterNull(jsonObject.getString("ID")));
			sellerInfo.setShopName(StringUtils.filterNull(jsonObject.getString("SHOP_NAME")));
			sellerInfo.setOnlinePay(StringUtils.filterBoolNull(jsonObject.getString("IS_PAY")));
			sellerInfo.setGoodNum(StringUtils.filterIntegerNull(jsonObject.getString("GOOD_NUM")));
			sellerInfo.setShopLogo(StringUtils.filterNull(jsonObject.getString("SHOP_LOGO")));
		} catch (JSONException e) {
			LogUtils.e(e.getMessage());
		}
	}

	/**
	 * @return the sellerInfo
	 */
	public SellerInfo getSellerInfo() {
		sellerInfo.setServiceOne(dayService.getServiceGoods());
		sellerInfo.setServiceOneDes(dayService.getServiceTime());
		sellerInfo.setServiceTwo(nightService.getServiceGoods());
		sellerInfo.setServiceTwoDes(nightService.getServiceTime());
		return sellerInfo;
	}
	
	private class LujiuService {
		/**
		 * 
		 */
		private String serviceTime;
		
		private List<Goods> serviceGoods;

		/**
		 * @return the serviceTime
		 */
		public String getServiceTime() {
			return serviceTime;
		}

		/**
		 * @param serviceTime the serviceTime to set
		 */
		public void setServiceTime(String serviceTime) {
			this.serviceTime = serviceTime;
		}

		/**
		 * @return the serviceDayGoods
		 */
		public List<Goods> getServiceGoods() {
			return serviceGoods;
		}

		/**
		 * @param serviceDayGoods the serviceDayGoods to set
		 */
		public void setServiceGoods(List<Goods> serviceDayGoods) {
			this.serviceGoods = serviceDayGoods;
		}
	}
}
